package com.luyuan.crm.member.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.model.MemberCoupon;
import com.luyuan.crm.member.model.MemberCouponRecord;
import com.luyuan.crm.member.model.MemberCouponType;
import com.luyuan.crm.member.service.MemberCouponRecordService;
import com.luyuan.crm.member.service.MemberCouponService;
import com.ruijie.foundation.dao.extend.support.HibernateEntityExtendDao;

public class MemberCouponServiceImpl extends HibernateEntityExtendDao<MemberCoupon> implements MemberCouponService {
    private MemberCouponRecordService memberCouponRecordService;

    @Override
    public void clearOverDateMemberCoupon() {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.YEAR, -2); // 年份减1
        Date lastMonth = ca.getTime(); // 结果
        logger.info("clean MemberIntegralRecord before: " + lastMonth);
        updateBySQL("memberCoupon.clearOverDateCoupon.record", new Date());
        update("delete MemberCoupon where expirationTime < ?", lastMonth);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MemberCoupon> findByMember(Member member) {
        return find("from MemberCoupon m where m.member = ?", member);
    }

    @Override
    public void addMemberCoupon(MemberCoupon memberCoupon, String description) {
        memberCoupon.setCreateTime(new Date());
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        // 两个月有效
        ca.add(Calendar.MONTH, +2);
        Date expirationTime = ca.getTime();
        memberCoupon.setId(null);
        memberCoupon.setExpirationTime(expirationTime);
        save(memberCoupon);

        MemberCouponRecord memberCouponRecord = new MemberCouponRecord();
        memberCouponRecord.setAmount(memberCoupon.getAmount());
        memberCouponRecord.setCouponType(memberCoupon.getCouponType());
        memberCouponRecord.setCreateTime(new Date());
        memberCouponRecord.setDescription("赠送A/B卷，请于两个月内使用：" + description);
        memberCouponRecord.setMember(memberCoupon.getMember());
        memberCouponRecordService.addMemberCouponRecord(memberCouponRecord);
    }

    @Override
    public void userMemberCoupon(Member member, MemberCouponType memberCouponType, int amount, String description) {
        MemberCouponRecord memberCouponRecord = new MemberCouponRecord();
        memberCouponRecord.setAmount(-amount);
        memberCouponRecord.setCouponType(memberCouponType);
        memberCouponRecord.setCreateTime(new Date());
        memberCouponRecord.setDescription(description);
        memberCouponRecord.setMember(member);
        memberCouponRecordService.addMemberCouponRecord(memberCouponRecord);

        // 已经使用的卷，直接删除
        @SuppressWarnings("unchecked")
        List<MemberCoupon> memberCoupons = find(
                "from MemberCoupon c where c.member =  ? and c.couponType = ? order by c.createTime asc", member,
                memberCouponType);

        for (MemberCoupon memberCoupon : memberCoupons) {
            amount -= memberCoupon.getAmount();
            if (amount >= 0) {
                remove(memberCoupon);
                break;
            }
            else {
                memberCoupon.setAmount(-amount);
                update(memberCoupon);
            }
        }
    }

    public void setMemberCouponRecordService(MemberCouponRecordService memberCouponRecordService) {
        this.memberCouponRecordService = memberCouponRecordService;
    }

}
